<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="12" >
            <a-form-item label="用户名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['username']" placeholder="请输入用户名"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="真实姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['realName']" placeholder="请输入真实姓名"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['sex']" :trigger-change="true" dictCode="sex" placeholder="请选择性别"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="用户手机" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['phone', validatorRules.phone]" placeholder="请输入用户手机"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="身份证号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['idCard']" placeholder="请输入身份证号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="用户邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['email', validatorRules.email]" placeholder="请输入用户邮箱"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="身份证照片" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload :isMultiple="false" v-decorator="['idCardUrl']"></j-image-upload>
            </a-form-item>
          </a-col>

          <a-col :span="12" >
            <a-form-item label="头像url" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload :isMultiple="false" v-decorator="['photoUrl']"></j-image-upload>
            </a-form-item>
          </a-col>

          <a-col :span="12" >
            <a-form-item label="会员等级" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="radioButton" v-decorator="['level',validatorRules.level]" :trigger-change="true" dictCode="customer_level" placeholder="请选择会员等级"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="是否团长" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <j-dict-select-tag type="radioButton" dictCode="yn" v-decorator="['teamLeaderFlag',validatorRules.teamLeaderFlag]" disabled :trigger-change="true" placeholder="请选择" style="width: 100%"/>
            </a-form-item>
          </a-col>
<!--          -->
          <a-col :span="12">
            <a-form-item label="开户姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['accountName']" placeholder="请输入开户姓名"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="银行卡号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['bankNo']" placeholder="请输入银行卡号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="开户行" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['bankName']" placeholder="请输入开户行"></a-input>
            </a-form-item>
          </a-col>
<!--          -->
          <a-col :span="12" >
            <a-form-item label="团长收益" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['commission']" placeholder="请输入团长收益" style="width: 100%" disabled :min="0"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="会员积分" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['integral']" placeholder="请输入会员积分" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="所属团长" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['inviteesId']" placeholder="请输入所属团长" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" style="position: relative" >
            <a-form-item label="邀请人" :labelCol="labelCol" :wrapperCol="wrapperCol">
<!--              <a-input v-decorator="['inviteCode_dictText']" placeholder="请输入所属团长" style="width: 100%"/>-->
<!--              style="position: absolute;width: 90%;height: 100%;top: -10px;left: 0;opacity: 1;"-->
              <j-popup
                v-decorator="['inviteCode_dictText']"
                :trigger-change="true"
                org-fields="id,realname"
                dest-fields="inviteCode,inviteCode_dictText"
                field="inviteCode_dictText"
                code="sysuser_select"
                :multi="false"
                @callback="popupCallback"
              />
            </a-form-item>
          </a-col>
          <!--<a-col :span="24" >
            <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['status']" :trigger-change="true" dictCode="status" placeholder="请选择" />
            </a-form-item>
          </a-col>
          <a-col :span="24" >
            <a-form-item label="实名认证审核" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['realnameStatus']" :trigger-change="true" dictCode="customer_status" placeholder="请选择实名认证审核"/>
            </a-form-item>
          </a-col>-->
        </a-row>
      </a-form>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="客户积分" :key="refKeys[0]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[0]"
          :loading="userIntegralRecordTable.loading"
          :columns="userIntegralRecordTable.columns"
          :dataSource="userIntegralRecordTable.dataSource"
          :maxHeight="300"
          :disabled="true"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import pick from 'lodash.pick'
  import { getAction } from '@/api/manage'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JImageUpload from '@/components/jeecg/JImageUpload'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: 'CustomerForm',
    mixins: [JEditableTableMixin],
    components: {
      JFormContainer,
      JImageUpload,
      JDictSelectTag,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          level: {
            initialValue:'1',
            rules: [
              { required: true, message: '请选择会员等级!'},
              { pattern: /^-?\d+\.?\d*$/, message: '请选择会员等级!'},
            ]
          },
          teamLeaderFlag: {
            initialValue:'0',
            rules: [
              { required: true, message: '请选择团长状态!'},
              { pattern: /^-?\d+\.?\d*$/, message: '请选择团长状态!'},
            ]
          },
          phone: {
            rules: [
              { required: false},
              { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!'},
            ]
          },
          email: {
            rules: [
              { required: false},
              { pattern: /^([\w]+\.*)([\w]+)@[\w]+\.\w{3}(\.\w{2}|)$/, message: '请输入正确的电子邮件!'},
            ]
          },
        },
        refKeys: ['userIntegralRecord', ],
        tableKeys:['userIntegralRecord', ],
        activeKey: 'userIntegralRecord',
        // 客户积分
        userIntegralRecordTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '操作',
              key: 'operateType',
              type: FormTypes.select,
              dictCode:"customer_record_operation",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '积分值',
              key: 'value',
              type: FormTypes.inputNumber,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '过期时间',
              key: 'expireTime',
              type: FormTypes.datetime,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '积分增减原因',
              key: 'reason',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        url: {
          add: "/member/customer/add",
          edit: "/member/customer/edit",
          queryById: "/member/customer/queryById",
          userIntegralRecord: {
            list: '/member/customer/queryUserIntegralRecordByMainId'
          },
        }
      }
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：false流程表单 true普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();

    },
    methods: {

      addBefore(){
        this.form.resetFields()
        this.userIntegralRecordTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'username','realName','sex','commission','phone','idCard','idCardUrl','teamLeaderFlag','email','photoUrl','integral','level','inviteesId','bankNo','bankName','accountName','inviteCode','inviteCode_dictText')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
          console.log(111)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.userIntegralRecord.list, params, this.userIntegralRecordTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          userIntegralRecordList: allValues.tablesValue[0].values,
        }
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          })
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
        console.log(row)
        this.form.setFieldsValue(pick(row,'inviteCode','inviteCode_dictText'))

       this.model.inviteCode = row.inviteCode
       if(this.model.inviteCode==''){
         this.model.inviteCode = null
       }
        console.log(this.model.inviteCode)

     },

    }
  }
</script>

<style scoped>
</style>